//
//  MateViewController.m
//  TQY
//
//  Created by 甘世清 on 2024/9/28.
//

#import "MateViewController.h"
#import "MateListViewController.h"
#import "BaseMyMateViewController.h"
#import "InfoSetUpViewController.h"
#import "IssueMateViewController.h"
#import "MateSetUpPopView.h"
#import "SQLocationPopView.h"
#import "SQCLLocationManager.h"
#import "VipPopView.h"
@interface MateViewController ()
@property (nonatomic , strong) MateSetUpPopView * popView;
@property (nonatomic , strong) MateListViewController * vc1;
@property (nonatomic , strong) MateListViewController * vc2;
@property (nonatomic , strong) MateListViewController * vc3;
@end

@implementation MateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self SQ_UIViewForSelfView];
    [SQLocationPopView SQ_Show:self.view];
}

- (void)SQ_UIViewForSelfView{
    [[SQCLLocationManager sharedManager] SQ_Location];
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"二级背景");
    [self.view addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        
    }];
    
    [self.view sendSubviewToBack:bgImageView];
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"淘搭子");
    label.textColor = UIColor.blackColor;
    label.font = FONT_MEDIUM(20);
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.view).offset(TopHeight-40);
    }];
    self.tabContentView.delegate = self;
    self.tabBar.backgroundColor = UIColor.clearColor;
    self.tabBar.leadingSpace = 10;
    self.tabBar.trailingSpace = 10;
    [self.tabBar setScrollEnabledAndItemFitTextWidthWithSpacing:10 minWidth:70];
    self.tabBar.indicatorColor  = SQColor3DA59D;
    [self.tabBar setIndicatorWidth:12 marginTop:40 marginBottom:7 tapSwitchAnimated:YES];
    [self setTabBarFrame:CGRectMake(0, TopHeight-10, SCREEN_WIDETH,50 )
       contentViewFrame:CGRectMake(0, TopHeight+40 , SCREEN_WIDETH, SCREEN_HEIGHT-TopHeight-FootHeight-80)];
    self.tabBar.itemTitleFont = FONT_REGULAR(16);
    self.tabBar.itemTitleSelectedFont =  FONT_MEDIUM(18);
    self.tabBar.itemTitleColor = SQColor(@"#999999");
    self.tabBar.itemTitleSelectedColor = SQColor3DA59D;
    [self.tabContentView setContentScrollEnabled:YES tapSwitchAnimated:YES];
    self.tabContentView.loadViewOfChildContollerWhileAppear = YES;
    self.viewControllers = [[NSMutableArray alloc]initWithObjects:self.vc1,self.vc2,self.vc3,nil];
    self.tabContentView.backgroundColor = UIColor.clearColor;
    
    WS(weakSelf);
    SQCustomButton * rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(20, 20) midmargin:0];
    rightBtn.image = SQGetImage(@"wodeliebiao");
    [self.view addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(40);
        make.right.equalTo(self.view.mas_right);
    }];
    [rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        UserModel * model = [UserModel share];
        if (model.gender > 0) {
            BaseMyMateViewController * vc = [[BaseMyMateViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else{
            InfoSetUpViewController * vc = [[InfoSetUpViewController alloc]init];
            vc.isEdit = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"该功能需要先完善个人信息方可使用!")];
            });
        }
    }];
    
    SQCustomButton * rightBtn2 = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(12, 12) midmargin:4];
    rightBtn2.image = SQGetImage(@"shaixuan");
    rightBtn2.text = SQNSLocalString(@"筛选");
    rightBtn2.font = FONT_MEDIUM(12);
    rightBtn2.textColor = SQColor3DA59D;
    [self.view addSubview:rightBtn2];
    [rightBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.tabBar);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(50);
        make.right.equalTo(self.view.mas_right).offset(-4);
    }];
    [rightBtn2 setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_popSetUpView];
    }];
    
    SQCustomButton * addBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(76, 76) midmargin:0];
    addBtn.image = SQGetImage(@"fabu");
    [self.view addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-10);
        make.size.mas_equalTo(76);
        make.bottom.equalTo(self.view.mas_bottom).offset(-10);
    }];
    [addBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        UserModel * model = [UserModel share];
        if (model.gender > 0) {
            IssueMateViewController * vc = [[IssueMateViewController alloc]init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }else{
            InfoSetUpViewController * vc = [[InfoSetUpViewController alloc]init];
            vc.isEdit = YES;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"该功能需要先完善个人信息方可使用!")];
            });
        }
        
    }];
   
}
-(void)SQ_popSetUpView{
    LSTPopView * pop = [LSTPopView initWithCustomView:self.popView
                                             parentView:nil
                                               popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    pop.avoidKeyboardSpace = -10;
    [pop pop];
    LSTPopViewWK(pop)
    [ pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    WS(weakSelf);
    [self.popView.yesBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_setType];
        [wk_pop dismiss];
    }];
}
-(void)SQ_setType{
    [self.vc1 SQ_SetMate:self.popView.seletedDic];
    [self.vc2 SQ_SetMate:self.popView.seletedDic];
    [self.vc3 SQ_SetMate:self.popView.seletedDic];
}

/**
 *  是否能切换到指定index
 */
- (BOOL)tabContentView:(YPTabContentView *)tabConentView shouldSelectTabAtIndex:(NSUInteger)index{
    if (index ==2) {
        if ([UserModel share].isVip) {
            if ([SQCLLocationManager sharedManager].authorizationStatus != kCLAuthorizationStatusDenied ){
                return YES;
            }
            [self SQ_HeaderViewqButtonClick];
            return NO;
        } else {
            [VipPopView SQ_Show];
            return NO;
        }
    }
    return YES;
}

-(void)SQ_HeaderViewqButtonClick{
   
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"设置定位权限") message:@"该功能需要访问您当前定位进行筛选,请开启定位后使用" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * co1 = [UIAlertAction actionWithTitle:SQNSLocalString(@"去开启") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
            }];
        }
    }];
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:co1];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

- (MateSetUpPopView *)popView{
    if (!_popView) {
        _popView = [[MateSetUpPopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT-200)];
    }
    return _popView;
}
- (MateListViewController *)vc1{
    if (!_vc1) {
        _vc1 = [[MateListViewController alloc]init];
        _vc1.type = @"0";
        _vc1.yp_tabItemTitle = SQNSLocalString(@"最新");
    }
    return _vc1;
}
- (MateListViewController *)vc2{
    if (!_vc2) {
        _vc2 = [[MateListViewController alloc]init];
        _vc2.type = @"1";
        _vc2.yp_tabItemTitle =  SQNSLocalString(@"热门");
    }
    return _vc2;
}
- (MateListViewController *)vc3{
    if (!_vc3) {
        _vc3 = [[MateListViewController alloc]init];
        _vc3.type = @"2";
        _vc3.yp_tabItemTitle =  SQNSLocalString(@"附近");
    }
    return _vc3;
}

@end
